package com.ramos.kuaixiao.db;

import android.content.ContentValues;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteStatement;
import android.util.Log;

import com.ramos.kuaixiao.App;
import com.ramos.kuaixiao.entity.Customer;

import java.util.ArrayList;
import java.util.List;

public class CustomerDB {
    private static String TAG = "CustomerDB";
    private static String TABLE_NAME = "Customer";
    public static final String CREATE_TABLE_SQL;
    public static final String DROP_USERINFO;

    static {
        CREATE_TABLE_SQL = "CREATE TABLE " + TABLE_NAME + " (" + "id" + " CHAR(30) PRIMARY KEY  NOT NULL,"
                + "qid" + " CHAR(30)," + "name" + " CHAR(30)," +"address" + " CHAR(30)," + "city" + " CHAR(30)," + "street" +
                " CHAR(30)," + "telephone" + " CHAR(30)," + "modifyTime" + " Long);";
        DROP_USERINFO = "DROP TABLE IF EXISTS " + TABLE_NAME;
    }

    public int addAllCustomer(List<Customer> customer) {
        SQLiteDatabase db = DBHelper.open();
        db.execSQL("DELETE FROM "+ TABLE_NAME);
        String sql = "insert into "+ TABLE_NAME+" (qid,id,name,address,city,street,telephone,modifyTime) values(?,?,?,?,?,?,?,?)";
        db.beginTransaction();
        Long a = System.currentTimeMillis();
        SQLiteStatement stmt = db.compileStatement(sql);
        for (int i = 0; i < customer.size(); i++) {
            stmt.bindString(1, customer.get(i).getUid() + "");
            stmt.bindString(2, customer.get(i).getId() + "");
            stmt.bindString(3, customer.get(i).getName() + "");
            stmt.bindString(4, customer.get(i).getAddress() + "");
            stmt.bindString(5, customer.get(i).getCity() + "");
            stmt.bindString(6, customer.get(i).getStreet() + "");
            stmt.bindString(7, customer.get(i).getMobilePhone() + "");
            stmt.bindString(8, customer.get(i).getModifyTime() + "");
            stmt.execute();
            stmt.clearBindings();
        }
        db.setTransactionSuccessful();
        db.endTransaction();
        App.mCustomerList.clear();
        App.mCustomerList.addAll(customer);
        Long b = System.currentTimeMillis();
        Log.i(TAG, String.valueOf(b - a));
        return 0;
    }

    public int updateAllCustomer(List<Customer> customer) {
        SQLiteDatabase db = DBHelper.open();
        db.beginTransaction();
        Long a = System.currentTimeMillis();
        for (int i = 0; i < customer.size(); i++) {
            Customer firstWarehouse = customer.get(i);
            ContentValues cv = new ContentValues();
            cv.put("id", firstWarehouse.getId() + "");
            cv.put("qid", firstWarehouse.getQid() + "");
            cv.put("name", firstWarehouse.getName() + "");
            cv.put("address", firstWarehouse.getAddress() + "");
            cv.put("city", firstWarehouse.getCity() + "");
            cv.put("street", firstWarehouse.getStreet() + "");
            cv.put("telephone", firstWarehouse.getTelephone() + "");
            cv.put("modifyTime", firstWarehouse.getModifyTime() + "");
            long l = db.insertWithOnConflict(TABLE_NAME, null, cv, SQLiteDatabase.CONFLICT_REPLACE); //如果有记录，则更新，并返回1，否则返回0
            Log.d(TAG, "updateAllCustomer:" + l + ","+firstWarehouse.getQid() + ""+","+firstWarehouse.getTelephone());
        }
        db.setTransactionSuccessful();
        db.endTransaction();
        Long b = System.currentTimeMillis();
        Log.i(TAG, String.valueOf(b - a));
        return 0;
    }

    public long getMaxModifyTime() {
        long param = 0;
        SQLiteDatabase db = DBHelper.open();
        Cursor cursor = db.rawQuery("select max(modifyTime) from "+ TABLE_NAME, new String[]{});
        Log.d(TAG, "getMaxModifyTime:getCount" + cursor.getCount());
        if (cursor.moveToFirst()) {
            param = cursor.getLong(0);
        }
        cursor.close();
        Log.d(TAG, "getMaxModifyTime:param" + param);
        return param;
    }


    public List<Customer> findAllCustomer() {
        List<Customer> list = new ArrayList<>();
        SQLiteDatabase db = DBHelper.open();
        Cursor cursor = db.rawQuery("select * from "+ TABLE_NAME, new String[]{});
        Log.d(TAG, "cursor:" + cursor.getCount());
        for (int i = 0; i < cursor.getCount(); i++) {
            cursor.moveToNext();
            Customer customer = new Customer();
            customer.setId(cursor.getString(0)+ "");
            customer.setQid(cursor.getString(1)+ "");
            customer.setName(cursor.getString(2)+ "");
            customer.setAddress(cursor.getString(3)+ "");
            customer.setCity(cursor.getString(4)+ "");
            customer.setStreet(cursor.getString(5)+ "");
            customer.setTelephone(cursor.getString(6)+ "");
            customer.setModifyTime(Long.valueOf(cursor.getString(7)));
            list.add(customer);
        }
        cursor.close();
        //db.close();
        Log.d(TAG, "findAllCustomer:" + list.size());
        return list;
    }

    public List<Customer> findCustomerName(String customer_name) {
        List<Customer> list = new ArrayList<>();
        SQLiteDatabase db = DBHelper.open();
        Cursor cursor = db.rawQuery("select * from "+ TABLE_NAME  +" where name =?", new String[]{customer_name});
        Log.d(TAG, "cursor:" + cursor.getCount());
        for (int i = 0; i < cursor.getCount(); i++) {
            cursor.moveToNext();
            Customer customer = new Customer();
            customer.setId(cursor.getString(0)+ "");
            customer.setQid(cursor.getString(1)+ "");
            customer.setName(cursor.getString(2)+ "");
            customer.setAddress(cursor.getString(3)+ "");
            customer.setCity(cursor.getString(4)+ "");
            customer.setStreet(cursor.getString(5)+ "");
            customer.setTelephone(cursor.getString(6)+ "");
            customer.setModifyTime(Long.valueOf(cursor.getString(7)));
            list.add(customer);
        }
        cursor.close();
        //db.close();
        Log.d(TAG, "findCustomerName:" + list.size());
        return list;
    }

    public List<Customer> findAllPageCustomer(int pageNow, int pageNum) {
        List<Customer> list = new ArrayList<>();
        SQLiteDatabase db = DBHelper.open();
        String limit = String.valueOf((pageNow - 1) * pageNum) + "," + String.valueOf(pageNum);
        Cursor cursor =
                db.query(TABLE_NAME, null, null, null, null, null, null, limit);//"5,9",第6行开始,返回9行数据
        //Cursor cursor = db.rawQuery("select * from Customer", new String[]{});
        Log.d(TAG, "cursor:" + cursor.getCount());
        for (int i = 0; i < cursor.getCount(); i++) {
            cursor.moveToNext();
            Customer customer = new Customer();
            customer.setId(cursor.getString(0)+ "");
            customer.setQid(cursor.getString(1)+ "");
            customer.setName(cursor.getString(2)+ "");
            customer.setAddress(cursor.getString(3)+ "");
            customer.setCity(cursor.getString(4)+ "");
            customer.setStreet(cursor.getString(5)+ "");
            customer.setTelephone(cursor.getString(6));
            customer.setModifyTime(Long.valueOf(cursor.getString(7)));
            list.add(customer);
        }
        cursor.close();
        //db.close();
        Log.d(TAG, "findAllPageCustomer:" + list.size());
        return list;
    }

    public void bindCustomer(String id, String telephone, String uid) {
        SQLiteDatabase db = DBHelper.open();
        db.execSQL("update "+TABLE_NAME +" set telephone=?,qid=? where id=?",
                new Object[]{telephone, uid, id});
    }


    public void deleteAll() {
        SQLiteDatabase db = DBHelper.open();
        db.execSQL("DELETE FROM "+ TABLE_NAME);
        Log.d(TAG, "deleteAllCustomer ");
    }

    public boolean hasData() {
        SQLiteDatabase db = DBHelper.open();
        Cursor cursor = db.rawQuery("select count(*)from "+TABLE_NAME, null);
        cursor.moveToFirst();
        Long count = cursor.getLong(0);
        if (count > 0) {
            cursor.close();
            Log.d(TAG, "hasCustomer true");
            return true;
        } else {
            Log.d(TAG, "hasCustomer false");
            cursor.close();
            return false;
        }
    }
}
